/**
	主逻辑管理
**/

var DestroyStarMediator = cc.Class({
    extends: cc.Component,
    name: 'DestroyStarMediator',
    properties: {
        columnArr:{
            default:[],
            type: cc.Node
        },
        star:{
            default:null,
            type: cc.Prefab
        },
        starPoolCount:{
            default:100
        },
        backgroundNode:{
            default:null,
            type: cc.Node
        },
        tipNode:{
            default:null,
            type: cc.Node
        },

    },
    onLoad: function () {
        this.createStarPool();
        this.initPanel();
        this.nodeList = {};
        this.destroyFinished = true;
        this.tipNodeMediator = this.tipNode.getComponent("TipNodeMediator");
        // this.columnArrCopy = cc.clone(this.columnArr);
    },
    //创建对象池
    createStarPool:function(){
        this.starPool = new cc.NodePool("StarMediator");
        for(let i = 0;i < this.starPoolCount; i++){
            this.starPool.put(cc.instantiate(this.star));
        }
    },
    //获取对象池内的星星节点
    getStar:function(){
        if(this.starPool.size() > 0){
            return this.starPool.get();
        }else{
            return cc.instantiate(this.star);
        }
    },
    //初始化面板
    initPanel:function(){
        for(let i in this.columnArr){
            for(let j = 0;j < 10;j++){
                var starNode = this.getStar();
                starNode.getComponent("StarMediator").setData(this);
                starNode.column = i;
                starNode.row = j;
                this.columnArr[i].addChild(starNode);
            }
        }
    },
    //补充被销毁的节点
    resetPanel:function(){
        for(let i in this.columnArr){
            var column = this.columnArr[i].children;
            for(let j = column.length;j < 10;j++){
                var starNode = this.getStar();
                starNode.getComponent("StarMediator").setData(this);
                starNode.column = i;
                starNode.row = j;
                this.columnArr[i].addChild(starNode);
            }
        }
    },
    //销毁后重新计算位置
    resetRowAndColumn:function(){
        for(let i in this.columnArr){
            var column = this.columnArr[i].children;            
            for(let j = 0;j < column.length;j++){
                column[j].column = i;
                column[j].row = j;
            }
        }
    },
    //把空列置后
    resetEmptyColumn:function(){
        //判断为空且不是最后一列则进行置后处理
        var emptyNodeArr = [],tempArr = [];
        for(let i = 0; i < this.columnArr.length;i++){
            var column = this.columnArr[i].children;
            var tempNode = this.columnArr[i];
            if(column.length == 0 && this.columnArr.length-1 != i && tempNode.active) {
                delete this.columnArr[i];
                tempNode.active = false;
                emptyNodeArr.push(tempNode);
            }
        }
        if(emptyNodeArr.length > 0){
            //获取下标从0开始的数组
            tempArr = this.getArrayByList(this.columnArr);
            //拼接两个数组
            tempArr.concat(emptyNodeArr);
            //对原数组进行重新赋值
            this.columnArr = tempArr;
        }
    },
    //递归查找符合要求的节点周围上下左右的节点是否符合，如果符合继续查找该节点周围几个
    getAroundNodeByTouchNode:function(touchNode){
        var colorTag = touchNode.colorTag,row = parseInt(touchNode.row),column = parseInt(touchNode.column);
        var columnNode = this.columnArr[column];
        var leftColumnNode = this.columnArr[column - 1];
        var rightColumnNode = this.columnArr[column + 1];
        var upNode = columnNode.children[row - 1],downNode = columnNode.children[row + 1];
        var leftNode = null,rightNode = null;
        //判断是否有左右节点
        if(leftColumnNode){
            leftNode = leftColumnNode.children[row];
        }
        if(rightColumnNode){
            rightNode = rightColumnNode.children[row];
        }
        
        var sign = column +"-"+ row;
        if(!this.nodeList[sign])  this.nodeList[sign] = touchNode;

        if(upNode && upNode.colorTag == colorTag) {
            var sign = upNode.column +"-"+ upNode.row;
            if(!this.nodeList[sign]) {
                this.nodeList[sign] = upNode;
                //递归查找
                this.getAroundNodeByTouchNode(upNode);
            }
        }
        upNode = null;
        
        if(downNode && downNode.colorTag == colorTag) {
            var sign = downNode.column +"-"+ downNode.row;
            if(!this.nodeList[sign]) {
                this.nodeList[sign] = downNode;
                //递归查找
                this.getAroundNodeByTouchNode(downNode);
            }
        }
        downNode = null;
        
        if(leftNode && leftNode.colorTag == colorTag) {
            var sign = leftNode.column +"-"+ leftNode.row;
            if(!this.nodeList[sign]) {
                this.nodeList[sign] = leftNode;
                //递归查找
                this.getAroundNodeByTouchNode(leftNode);
            }
        }
        leftNode = null;
        
        if(rightNode && rightNode.colorTag == colorTag) {
            var sign = rightNode.column +"-"+ rightNode.row;
            if(!this.nodeList[sign]) {
                this.nodeList[sign] = rightNode;
                //递归查找
                this.getAroundNodeByTouchNode(rightNode);
            }
        }
        rightNode = null;
        //如果没有符合的则逐层递归
        if(!upNode && !downNode && !leftNode && !rightNode) return this.nodeList;
    },
    selectPointColor:function(touchNode){
        //上一次销毁没结束，则不能进行下一次销毁
        if(!this.destroyFinished) return;
        var self = this;
        //判断两次选中的是否一致，如果是则返回消灭的数量，不是返回false
        var result = this.judgeTowTimesSelectNodeIsSame(touchNode);
        if(result){
            //得分消灭选中的星星
            this.tipNodeMediator.addScore(result);
            var starNodeArr = this.getArrayByList(this.nodeList);
            this.destroyStarNode(starNodeArr,function(){
                //空列置后
                self.resetEmptyColumn();
                //重新赋值行列坐标
                self.resetRowAndColumn();
                var isOver = self.checkGameIsOver();
                if(isOver) self.gameOver();
            });
            return;
        }
        //恢复之前选中的星星状态
        this.resumeSelectStarNode();
        var list = this.getAroundNodeByTouchNode(touchNode);
        var size = this.getListSize(list);
        //如果周围有两个以上相同节点，则全部变成选中态
        if(size > 1) {
            for(let i in list){
                list[i].getComponent("StarMediator").setSelectSpEnable(true);
            }
            //设置得分
            this.tipNodeMediator.setSelectNum(size);
        }
    },
    //消毁选中的星星
    destroyStarNode:function(nodeArr,cb){
        var self = this;
        this.destroyFinished = false;
        for(let i in nodeArr){
            let starNode = nodeArr[i];
            //播放完销毁动画后，回收节点
            starNode.getComponent("StarMediator").blinkAction(function(){
                self.starPool.put(starNode);
                //销毁到最后一个，进入下一步操作
                if(i == nodeArr.length - 1) {
                    self.destroyFinished = true;
                    cb();
                }
            });
        }
        this.nodeList = {};
    },
    //恢复星星为没选中状态
    resumeSelectStarNode:function(){
        for(let i in this.nodeList){
            let starNode = this.nodeList[i];
            starNode.getComponent("StarMediator").setSelectSpEnable(false);
        }
        this.nodeList = {};
    },
    //判断两次选中的节点是否一致
    judgeTowTimesSelectNodeIsSame:function(touchNode){
        var size = this.getListSize(this.nodeList);
        var sign = touchNode.column +"-"+ touchNode.row;
        if(size > 1 && this.nodeList[sign]) return size;
        return false;
    },
    checkGameIsOver:function(){
        //检测游戏是否结束，判断如果还有两个相连的节点，则没结束
        for(let i in this.columnArr){
            var column = this.columnArr[i].children;            
            for(let j = 0;j < column.length;j++){
                var list = this.getAroundNodeByTouchNode(column[j]);
                var size = this.getListSize(list);
                //清空本次查找到的数据
                this.nodeList = {};
                if(size > 1) return false;
            }
        }
        return true;
    },
    gameOver:function(){
        //游戏结束后，重置场景
        var self = this;
        this.reuseAllStarNode(function(){
            self.nextLevel();
        });
    },
    //回收剩下的全部节点
    reuseAllStarNode:function(cb){
        var residueStarArr = this.getAllResidueStarNode();
        this.destroyStarNode(residueStarArr,cb);
    },
    //获得所有剩余的节点
    getAllResidueStarNode:function(){
        var tempArr = [];
        for(let i in this.columnArr){
            let column = this.columnArr[i].children;            
            for(let j = 0;j < column.length;j++){
                tempArr.push(column[j]);
            }
        }
        return tempArr;
    },
    nextLevel:function(){
        var self = this;
        //在游戏中有把空列置后，所以顺序打乱了，现在要重新置回
        this.columnArr = [];
        for(let i in this.node.children){
            this.node.children[i].active = true;
            this.columnArr.push(this.node.children[i]);
        }
        this.initPanel();
        this.tipNodeMediator.setLevelTip();
    },
    getListSize:function(list){
        var arr = this.getArrayByList(list);
        return arr.length;
    },
    getArrayByList:function(list){
        var tempArr = [];
        for(let i in list){
            tempArr.push(list[i]);
        }
        return tempArr;
    },
});

module.exports = DestroyStarMediator;